<?php 
	namespace Home\Controller;
	use Think\Controller;
	use Home\Model\RolePermissionModel;
	use Home\Model\UserProvinceModel; 
	use Home\Model\ProvinceModel; 

	class AuthController extends Controller{

		public $sys_userid;
		public $sys_username;
		public $sys_user_roleid;

		public function _initialize()
		{
			if(strtolower(CONTROLLER_NAME)=='public')return;
			$sys_userinfo = session('sys_userinfo');
			// 判断是否已登录
			if($sys_userinfo){
				$user = unserialize($sys_userinfo);
				if($user){
					$userinfo = unserialize($sys_userinfo);
					$this->sys_userid = $userinfo['id'];
					$this->sys_username = $userinfo['username'];
					$this->sys_user_roleid = $userinfo['role_id'];
					$s = array(
						"sys_userid" => $this->sys_userid,
						"sys_username" => $this->sys_username,
						"sys_user_roleid"=> $this->sys_user_roleid
						);
					 // $output['userdata'] = $s;
				     // $output = $this->getPermiss();
					 // $output['result'] = $this->getState("0");	
					 // echo json_encode($output);
				}else{
					 session(null);
					 $output['result'] =  $this->getState("3");
					 echo json_encode($output); exit;
				}				
			}else{
				 $output['result'] =  $this->getState("5");
				 echo json_encode($output); exit;
			}
		}

		// 通用状态码输出
		public function getState($code)
		{
			$s = array(
				"-1"=>"账号或密码错误",
				"0" =>"登录成功",
				"1" =>"账号失效",
				"2" =>"最近操作时间更新失败",
				"3" =>"退出成功",
				"4" =>"没有目标地址访问权限",
				"5" =>"用户未登录",
				"6" =>"没有可访问的地址",
				"7" =>"节目信息获取成功",
				"8" =>"登录界面",
				"9" =>"获取省份信息的成功"
			);
			return array("code"=>intval($code),"message"=>strval($s[$code]));
		}


		public function getPermiss($role_id =null)
		{
			$role_id = $role_id?$role_id:$this->sys_user_roleid;
			$per = unserialize(session('sys_per'));
	        if(!$per)
		        {
		            $per = D("RolePermission")->get_permiss($role_id);
					session('sys_per',serialize($per));
		        }
	        $per_u = unserialize(session('sys_per_u'));
	        if(!$per_u)
	        {
	            $per_u = array();
	            foreach($per as $v)
	            {
	                $per_u[] = $v['url'];
	                if(isset($v["sub"]))
	                {
	                    foreach($v["sub"] as $vv)
	                    {
	                        $per_u[] = $vv['url'];
	                    }
	                }
	            }
				session('sys_per_u',serialize($per_u));
	        }
	        // dump($per_u);exit();
	        $current_controller_action = strtolower(CONTROLLER_NAME."/".ACTION_NAME);
	        // dump($current_controller_action);
	     	// dump($per_u);exit();
	        if($this->filter() || in_array($current_controller_action,$per_u))
	        {
	        	$res = $this->get_tree($per);  //获取树节点
	        	// dump($res);exit;
	            return $res;
	        }else{
	        	// return false;
	            session(null);
	            return false;
	            // $output['result'] = $this->getState("4");
	            // echo json_encode($output);
	        }
		}

		// 数据处理  返回前端适用数据
		private function get_tree($per)
		{
			// 挑选出满足条件的数据
			foreach ($per as $key => $value) {
				if($value['type'] == 0){
					$data[] = $value;
				}
			}
			foreach ($data as $key => $value) {
				$output[$key]['id'] = $value['id'];
				$output[$key]['text'] = $value['permission_name'];
				$output[$key]['attributes'] = array("url"=>$this->get_url($value['permission_name']));
				$output[$key]['parentid'] = $value['pid'];
			}
			return $output;
		}

		private function get_url($permission_name)
		{
			switch ($permission_name) {
				case '节目管理':
					return "jiemu.php";
					break;
				case '用户管理':
					return "yonghu.php";
					break;
				case '省份管理':
					return "shengfen.php";
					break;
				case '角色管理':
					return "juese.php";
					break;
				case '权限管理':
					return "quanxian.php";
					break;
				case '下载统计':
					return "tongji.php";
					break;
				case '下发记录':
					return "log.php";
				break;
				case '下载监控':
					return "jiankong.php";
				break;
				default:
					# code...
					break;
			}
		}

		public function filter()
   		{
	        $f = array(
	            "Public"=>"",
	            "Sys"=>array("pwd")
	        );
	        if(isset($f[CONTROLLER_NAME]))
	        {
	            if(empty($f[CONTROLLER_NAME]))
	            {
	                return true;
	            }else{
	                return in_array(ACTION_NAME,$f[CONTROLLER_NAME])?true:false;
	            }
	        }else{
	            return false;
	        }
	    }
	}
 ?>